﻿using App.Chat.Auth;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Zws.MongoDb;

namespace App.Chat.Hubs
{
    [NeedAuth]
    public class ChatHub : AbsChatHub, IChatHub
    {
        private readonly IHubContext<ChatHub> _hubContext;
        public ChatHub(IHttpContextAccessor contextAccessor,
            IHubContext<ChatHub> hubContext,
            MongoDbClientFacade mongoDbClient) : base(contextAccessor, mongoDbClient)
        {
            _hubContext = hubContext;
        }
        /// <summary>
        /// 修改当前会话用户
        /// </summary>
        /// <param name="sessionUserId"></param>
        /// <returns></returns>
        //public async Task ChangeCurrentSessionIdAsync(string sessionUserId)
        //{
        //    await _mongoDbClient.UpdateOneAsync<OnlineUserConnect, string>(x => sessionUserId.Equals(x.UserId), x => x.SessionId, sessionUserId);
        //}

        //public async Task DeleteSessionAsync(string sessionUserId)
        //{
        //    //删除会话同时删除聊天记录
        //    await _mongoDbClient.DeleteAsync<SessionUsers>(x => _currentUser.Id.Equals(x.UserId)
        //    && sessionUserId.Equals(x.SessionId));

        //}

        //public async Task SendMsgToFriendAsync(string sessionUserId, string message)
        //{
        //    var connId = await GetConnIdByUserIdAsync(sessionUserId);
        //    //判断对方的会话列表中是否存在跟发送方的会话，没有则进行添加并增加未读消息
        //    var isExistSession = await _mongoDbClient.IsExistAsync<SessionUsers>(x =>
        //    _currentUser.Id.Equals(x.SessionId) && sessionUserId.Equals(x.UserId));

        //    if (!isExistSession)
        //        await _mongoDbClient.InsertAsync<SessionUsers>(new SessionUsers
        //        {
        //            LastMsg = message,
        //            UnReadMsgCount = 1,
        //            SessionId = _currentUser.Id,
        //            SessionName = _currentUser.UserName,
        //            UserId = sessionUserId,
        //        });
        //    else
        //        await _mongoDbClient.UpdateOneAsync<SessionUsers>(x => sessionUserId.Equals(x.UserId) && _currentUser.Id.Equals(x.SessionId),
        //        builder =>
        //        {
        //            var updateDefinition = builder.Set(x => x.LastMsg, message)
        //                   .CurrentDate(x => x.RecTime);
        //            //如果对方不在线才增加未读消息，否则不增加
        //            if (connId == null)
        //                return updateDefinition.Inc(x => x.UnReadMsgCount, 1);
        //            return updateDefinition;
        //        }
        //        );
        //    if (connId != null)
        //        await _hubContext.Clients.Client(connId).SendAsync("ReciveMsg", _currentUser.Id, message);
        //}
        //public async Task FlushSessionList(string userId)
        //{
        //    var connId = await GetConnIdByUserIdAsync(userId);
        //    await _hubContext.Clients.Client(connId).SendAsync("FlushSessionList");
        //}
        public async Task SendMsgToSpecialClient(string connId, string method, params object[] objects)
        {
            await _hubContext.Clients.Client(connId).SendAsync(method, _currentUser.Id, objects);
        }
        public async Task SendMsgToSpecialClients(List<string> connIdList, string method, params object[] objects)
        {
            await _hubContext.Clients.Clients(connIdList).SendAsync(method, objects);
        }
    }
}
